

public class Solution {
    class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
    /*
    迭代解法
     */
    public ListNode reverseList(ListNode head) {
            ListNode curr=head;
            ListNode prev= null;

            while(curr!=null){
                ListNode next=curr.next;
                curr.next=prev;
                prev=curr;
                curr=next;
            }
            return prev;
    }
    /*
    递归解法
     */
    public  ListNode reverseList2(ListNode head){
        if(head==null||head.next==null){
            return head;
        }
        ListNode newHead=reverseList2(head.next);
        head.next.next=head;
        head.next=null;
        return newHead;
    }
}
